<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <script>
    // 最快
    // const lengthOfLongestSubstring = (s) => {
    //   let max = s ? 1 : 0
      
    //   for (let i = 0; i < s.length; i++) {
    //     let code = s[i]
    //     for (let j = i + 1; j < s.length; j++) {          
    //       if(code.indexOf(s[j]) === -1) {
    //         code = `${code}${s[j]}`
    //         max = max < code.length ? code.length : max
    //         if(code.length === s.length) {
    //           max = max < code.length ? code.length : max
    //           break
    //         }
    //       } else {
    //         max = max < code.length ? code.length : max
    //         break
    //       }
    //     }
    //   }
    //   return max
    // }
    const lengthOfLongestSubstring = (s) => {
      let max = 0, i = 0
      let temp = []
      while(i < s.length) {
        if(temp.indexOf(s[i]) === -1) {
          temp.push(s[i])
        } else {
          temp.shift()
          continue
        }
        max = Math.max(max, temp.length)
        i++
      }
      return temp
    }
    
    console.log(lengthOfLongestSubstring('abca'));
    
    
  </script>
</body>
</html>